package com.fick.tags;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.News;
import com.fick.util.General;
/**
 * 显示指定栏目信息
 * @author 王浩强
 * 2009-08-14
 * 
 * **/
public class NewsFromFirstTag extends TagSupport {
	private static Log log = LogFactory.getLog(NewsFromFirstTag.class);
	//栏目id
	private int channelid=0;
	//从第几条开始
	private int beginNum=0;
	//显示几条
	private int num=10;
	//标题长度
	private int length=30;
	//table属性
	private String tableAttr="";
	//tr属性
	private String trAttr="";
	//dot td 属性
	private String dotTdAttr="";
	//title td 属性
	private String titleTdAttr="";
	//date td 属性
	private String dateTdAttr="";
	//dot 图片
	private String dotSrc="";
	//dot 图片属性
	private String dotAttr="";
	//<a>标签href页面
	private String linkPage="";
	//<a>标签属性
	private String linkAttr="";
	//是否显示日期
	private boolean isDate=true;
	//日期格式
	private String dateFormat="yyyy-MM-dd";
	//是否显示子栏目
	private boolean isChild=false;
	
	//子栏目标题属性
	private String clikTdAttr="";
	private String spanAttr="";
	
	//是否显示点击量
	private boolean isCliknum=false;
	
	private boolean isSign=false;   //是否签收
	
	public int doStartTag() throws JspException {
			
		try {
			StringBuffer sb = new StringBuffer();
			NewsDAO dao=new NewsDAO();
			List<News> list=null;
			if(isChild){
				list=dao.findPageFirstByHql("where isAss='1' and channelid.channelId in(select channelId from Channel where fatherId="+channelid+") order by isTop desc addDate desc", num, beginNum);
			}else{
				list=dao.findPageFirstByHql("where isAss='1' and channelid.channelId="+channelid+" order by isTop desc addDate desc", num, beginNum);
			}
			
			if (list!=null && list.size()>beginNum) {
				News news;
				sb.append("<table ");
				sb.append(tableAttr);
				sb.append(" ><tbody>");
				for (int i = 0; i < list.size(); i++) {
					news=list.get(i);
					sb.append("<tr ");
					sb.append(trAttr);
					sb.append(" >");
					sb.append("<td ");
					sb.append(dotTdAttr);
					sb.append(" >");
					if (dotSrc.trim().length()>0) {
						sb.append("<img src='");
						sb.append(dotSrc);
						sb.append("' ");
						sb.append(dotAttr);
						sb.append(" />");
					}
					sb.append("</td>");
					sb.append("<td ");
					sb.append(titleTdAttr);
					sb.append(" >");
					if(isChild)
					{
						sb.append("[<span ");
						sb.append(spanAttr);
						sb.append(">");
						sb.append("<a href=\"#\">"+news.getChannelid().getName());
						sb.append("</a></span>] ");
					}
					if (news.getFilePath()!=null && news.getFilePath().trim().length()>0) {
						sb.append("<a href='/downloadFile.jsp?filename=");
						sb.append(java.net.URLEncoder.encode(news.getFilePath(),"UTF-8"));
						sb.append("' >");
					}else
					{
					sb.append("<a href='");
					sb.append(linkPage);
					sb.append("?channelid=");
					sb.append(channelid);
					sb.append("&newsid=");
					sb.append(news.getNewId());
					sb.append("' ");
					sb.append(linkAttr);
					sb.append(" >");
					}
					sb.append(General.trimStringInLength(news.getTitle(), length));
					SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); 
					long day=0;
					try{
						java.util.Date date= myFormatter.parse(myFormatter.format(new Date())); 
					    java.util.Date mydate= myFormatter.parse(myFormatter.format(news.getAddDate())); 
						day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
					}catch(Exception er){er.printStackTrace();}
					if(day<=2)
					{
						sb.append("<img src='images/new.gif'>");
					}
					sb.append("</a>");
					sb.append("</td>");
					if(isSign)
					{
						sb.append("<td ");
						sb.append(spanAttr);
						sb.append(">");
						if("1".equals(news.getIsSign()))//为1时表示信息需要签收，为0表示不需要签收
						{
							
							sb.append("<a href=\"#\" onclick=\"window.open(\'/signLogin.jsp?newsId="+news.getNewId()+"\','newwindow',\'height=600,width=340,status=no,top=100,left=300\')\">");
							sb.append("[签收]</a>");
						} else {
							sb.append("[签收完毕]");
						}
						sb.append("</td>");
					}
					if(isCliknum)
					{
						sb.append("<td ");
						sb.append(clikTdAttr);
						sb.append(">");
						sb.append("["+news.getClickNum()+"]");
						sb.append("</td>");
					}
					if (isDate) {
						sb.append("<td ");
						sb.append(dateTdAttr);
						sb.append(" >");
						SimpleDateFormat df = new SimpleDateFormat(dateFormat);
						if(day<=1){
							sb.append("<font color=\"red\">"+df.format(news.getAddDate())+"</font>");
						}else{
							sb.append(df.format(news.getAddDate()));
						}
						sb.append("</td>");
					}
					sb.append("</tr>");
				}
				sb.append("</tbody></table>");
			}
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
			out.flush();
		}catch(IOException e)
		{
			log.error("标签显示错误"+e);
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	public int getChannelid() {
		return channelid;
	}
	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}
	public int getBeginNum() {
		return beginNum;
	}
	public void setBeginNum(int beginNum) {
		this.beginNum = beginNum;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getTableAttr() {
		return tableAttr;
	}
	public void setTableAttr(String tableAttr) {
		this.tableAttr = tableAttr;
	}
	public String getTrAttr() {
		return trAttr;
	}
	public void setTrAttr(String trAttr) {
		this.trAttr = trAttr;
	}
	public String getDotTdAttr() {
		return dotTdAttr;
	}
	public void setDotTdAttr(String dotTdAttr) {
		this.dotTdAttr = dotTdAttr;
	}
	public String getTitleTdAttr() {
		return titleTdAttr;
	}
	public void setTitleTdAttr(String titleTdAttr) {
		this.titleTdAttr = titleTdAttr;
	}
	public String getDateTdAttr() {
		return dateTdAttr;
	}
	public void setDateTdAttr(String dateTdAttr) {
		this.dateTdAttr = dateTdAttr;
	}
	public String getDotSrc() {
		return dotSrc;
	}
	public void setDotSrc(String dotSrc) {
		this.dotSrc = dotSrc;
	}
	public String getDotAttr() {
		return dotAttr;
	}
	public void setDotAttr(String dotAttr) {
		this.dotAttr = dotAttr;
	}
	public String getLinkPage() {
		return linkPage;
	}
	public void setLinkPage(String linkPage) {
		this.linkPage = linkPage;
	}
	public String getLinkAttr() {
		return linkAttr;
	}
	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}
	public boolean getIsDate() {
		return isDate;
	}
	public void setIsDate(boolean isDate) {
		this.isDate = isDate;
	}
	public String getDateFormat() {
		return dateFormat;
	}
	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}
	public boolean getIsChild() {
		return isChild;
	}
	public void setIsChild(boolean isChild) {
		this.isChild = isChild;
	}
	public String getSpanAttr() {
		return spanAttr;
	}
	public void setSpanAttr(String spanAttr) {
		this.spanAttr = spanAttr;
	}
	public String getClikTdAttr() {
		return clikTdAttr;
	}
	public void setClikTdAttr(String clikTdAttr) {
		this.clikTdAttr = clikTdAttr;
	}
	public boolean getIsCliknum() {
		return isCliknum;
	}
	public void setIsCliknum(boolean isCliknum) {
		this.isCliknum = isCliknum;
	}
	public boolean getIsSign() {
		return isSign;
	}
	public void setIsSign(boolean isSign) {
		this.isSign = isSign;
	}
}
